package com.ag.hmk.action;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.ag.bean.TbLaw;
import com.ag.bean.TbLkw;
import com.ag.common.IDUtil;
import com.ag.common.PageUtil;
import com.ag.hmk.service.impl.HmkUiltServImpl;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 
 * @author hmk
 * 查询法规列表（分页查询）
 */
public class FindSomeLawAction extends ActionSupport {
	private static final long serialVersionUID = -4270657116058210604L;
	
	private HmkUiltServImpl hmkUiltServImpl;
	private int page;
	private int maxRow;
	private String typeID1;
	private String lawNature1;
	private String lawName1;
	private String lawDetail1;
	
	private String kwstr1="kw5";
	
	private PageUtil pageUtil; //返回结果
	private List<String> lawKeywordList; //返回关键字
	
	
	public String findLawList()throws Exception{
		List<String> strList = new ArrayList<String>();
		PageUtil pUtil = new PageUtil();
		List result=null;
		int total=0;
		try {
			String cql = "select count(*) from TbLaw l  where 1=1  ";
			String hql = "from TbLaw l left join fetch l.tbType left join fetch l.tbLawnature left join fetch l.tbLkws k  where 1=1";
			System.out.println("----"+typeID1);
			if (typeID1!=null && typeID1.length()>0) {
				cql += " and l.tbType.tpyeId = " + typeID1;
				hql += " and l.tbType.tpyeId = " + typeID1;
			}
			System.out.println("----"+lawNature1);
			if (lawNature1!=null && lawNature1.length()>0) {
				cql += " and l.tbLawnature.lnId = " + lawNature1;
				hql += " and l.tbLawnature.lnId = " + lawNature1;
			}
			System.out.println("----"+lawName1);
			if (lawName1!=null && lawName1.length()>0) {
				cql += " and l.lawName like '%" + lawName1+"%'";
				hql += " and l.lawName like '%" + lawName1+"%'";
			}
			
			System.out.println("----"+lawDetail1);
			if (lawDetail1!=null && lawDetail1.length()>0) {
				hql += " and k.lawKw like '%" + lawDetail1+"%'";
			}
			
			cql +=" and l.a='"+IDUtil.PRESERVE+"' ";
			hql+=" and l.a='"+IDUtil.PRESERVE+"' order by l.lawId desc";
			
			if (page == 0) {
				page = 1;
			}
			if (maxRow == 0) {
				maxRow = 10;
			}
			int firstRow = (page - 1) * maxRow;
			//查询法规列表（分页查询）
			result = hmkUiltServImpl.query(hql, firstRow, maxRow);
			if (result!=null) {
					Iterator it1 = result.iterator();
					total =result.size();
					while(it1.hasNext()){
						String str1 = "";
						int i = 0;
						TbLaw lawtest1 = (TbLaw)it1.next();
						Set<TbLkw> lkwSet = null ;
						lkwSet=( Set<TbLkw>)lawtest1.getTbLkws();
						Iterator<TbLkw> it2 =lkwSet.iterator();
						while(it2.hasNext()){
							//System.out.println("---keyworjfjfjfjfds:"+it2.next().getLawKw());
							str1 += (it2.next().getLawKw()+",");
							i++;
							if (i>4) {
								break;
							}
						}
						str1 +="...";
						strList.add(str1);
						System.out.println("---------list:"+str1);
					}
				} else {
					System.out.println("结果为空");
				}
		} catch (Exception e) {
			e.printStackTrace();
			result = null;
			total = 0;
			strList = null;
		}
		pUtil.setTotal(total);
		pUtil.setPage(page);
		pUtil.setMaxRow(maxRow);
		pUtil.setList(result);
		
		this.setLawKeywordList(strList);
		this.setPageUtil(pUtil);
		return "findLawListOK";
	}

	public void setHmkUiltServImpl(HmkUiltServImpl hmkUiltServImpl) {
		this.hmkUiltServImpl = hmkUiltServImpl;
	}


	public PageUtil getPageUtil() {
		return pageUtil;
	}


	public void setPageUtil(PageUtil pageUtil) {
		this.pageUtil = pageUtil;
	}


	public void setPage(int page) {
		this.page = page;
	}


	public void setMaxRow(int maxRow) {
		this.maxRow = maxRow;
	}

	public void setTypeID1(String typeID1) {
		this.typeID1 = typeID1;
	}

	public void setLawNature1(String lawNature1) {
		this.lawNature1 = lawNature1;
	}

	public void setLawName1(String lawName1) {
		this.lawName1 = lawName1;
	}

	public void setLawDetail1(String lawDetail1) {
		this.lawDetail1 = lawDetail1;
	}

	public List<String> getLawKeywordList() {
		return lawKeywordList;
	}

	public void setLawKeywordList(List<String> lawKeywordList) {
		this.lawKeywordList = lawKeywordList;
	}

	
	

}
